package com.lly.schedule.dao;

import com.lly.schedule.util.JDBCUtil;

import java.lang.reflect.Field;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BaseDao {

    //公共的查询方法  返回的是单个对象
    public <T> T baseQueryObject(Class<T> clazz, String sql, Object ... args) throws Exception {
        Connection  connection = null;
        PreparedStatement preparedStatement=null;
        ResultSet  resultSet=null;
        T t=null;
        try {
             connection = JDBCUtil.getConnection();
             preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i+1, args[i]);
            }
             resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                 t = (T)resultSet.getObject(1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
                if(resultSet!=null){
                    try{
                        resultSet.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                if(preparedStatement!=null){
                    try {
                        preparedStatement.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            JDBCUtil.closeConnection();
        }
        return t;
    }
    public <T> List<T> baseQuery(Class<T> clazz, String sql, Object ... args) throws Exception {
        Connection  connection = null ;
        PreparedStatement preparedStatement=null;
        ResultSet  resultSet=null;
        ArrayList<T> list=new ArrayList<>();
        try {
            connection=JDBCUtil.getConnection();
            preparedStatement=connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i+1,args[i]);
            }
            resultSet=preparedStatement.executeQuery();
            //获取元数据,可以得到每列名字和列数
            ResultSetMetaData metaData = resultSet.getMetaData();
            while(resultSet.next()){
                T t = clazz.newInstance();
                for (int i = 1; i <=metaData.getColumnCount(); i++){
                    String columnLabel = metaData.getColumnLabel(i);
                    Object object = resultSet.getObject(i);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,object);
                }
                list.add(t);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (null !=resultSet) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (null != preparedStatement) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            JDBCUtil.closeConnection();
        }
        return list;
    }
    // 通用的增删改方法
    public int baseUpdate(String sql,Object ... args) throws Exception {
        // 获取连接
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement preparedStatement=null;
        int rows = 0;
        try {
            // 准备语句对象
            preparedStatement = connection.prepareStatement(sql);
            // 设置语句上的参数
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i+1,args[i]);
            }

            // 执行 增删改 executeUpdate
            rows = preparedStatement.executeUpdate();
            // 释放资源(可选)


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (null != preparedStatement) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

            }
            JDBCUtil.closeConnection();
        }
        // 返回的是影响数据库记录数
        return rows;
    }
}
